Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
fetching data through env.local file gives an error "Failed to load resource: the server responded with a status of 401"

I have a problem when i try to fetch data through my env file i get an error "Failed to load resource: the server responded with a status of 401 ()" error msg

:{
    "status_code": 7,
    "status_message": "Invalid API key: You must be granted a valid key.",
    "success": false
}

but when i fetch data without using env.local file and use my api key directly in const variable everything works fine. But i want to make the env local file call to work.

My env.local file

API_KEY=3retkjdkfgjfd95659 (MADE IT UP FOR THE POST)

My utils folder /requests.js

const API_KEY = process.env.API_KEY;
//const API_KEY = "3retkjdkfgjfd95659" the code works only when i use this variable
const requests = {
  fetchTopRated: {
    title: "Top Rated",
    url: `/movie/top_rated?api_key=${API_KEY}&language=en-US`,
  },}

My axios file

const instance = axios.create({
  baseURL: "https://api.themoviedb.org/3/",
});

export default instance;

Banner file

function Banner() {
  const [movie, setMovie] = useState([]);
  useEffect(() => {
    async function fetchData() {
      const request = await axios.get(requests.fetchTopRated.url);
      setMovie(
        request.data.results[
          Math.floor(Math.random() * request.data.results.length - 1)
        ]
      );
      return request;
    }
    fetchData();
  }, []);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!